package com.xnew.common.response;

import com.alibaba.fastjson.JSONPObject;
import com.alibaba.fastjson.annotation.JSONType;
import com.xnew.common.response.status.ResponseCode;
import com.xnew.common.response.status.Status;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;

/**
 * @ClassName BaseResponse
 * @Description TODO
 * @Author tinx
 * @Date 2019/3/11 0011 下午 4:15
 * @Version 1.0
 **/
@Data
@JSONType(orders = {"code","msg","data"})
public class BaseResponse<T> implements Response, Serializable {

    private static final ResponseCode SUCCESS = ResponseCode.SUCCESS;
    /**
     * 状态码
     */
    private int code;
    /**
     * 消息(错误）
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;

    private Status status;

    private int count;



    public BaseResponse() {
        this.code = SUCCESS.getCode();
        this.msg = SUCCESS.getMsg();
    }

    public BaseResponse(int code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(T data) {
        this();
        this.data = data;
    }

    public BaseResponse(Status status) {
        this();
        this.status = status;
    }


    /**
     * JSONP跨域数据格式输出
     * @param callbackFunction JSONP回调函数名
     * @return callbackFunction(BaseResponse)
     */
    @Deprecated
    public final JSONPObject toJSONP(String callbackFunction) {
        JSONPObject jsonpObject = new JSONPObject(callbackFunction);
        jsonpObject.addParameter(this);
        return jsonpObject;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("code", code)
                .append("msg", msg)
                .append("data", data)
                .append("count",count)
                .toString();
    }


}
